﻿using System;

namespace SBToolkit.Core
{
    /// <summary>
    /// Testable version of <see cref="DateTime"/>.
    /// </summary>
    public static class SystemDateTime
    {
        /// <summary>
        /// Initializes static members of the <see cref="SystemDateTime"/> class. 
        /// </summary>
        static SystemDateTime()
        {
            Now = () => DateTime.Now;
        }

        /// <summary>
        /// Gets or sets current <see cref="DateTime"/>.
        /// </summary>
        public static Func<DateTime> Now
        {
            get;
            set;
        }

        /// <summary>
        /// Creates the specified date with time.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <param name="hour">The hour.</param>
        /// <param name="minute">The minute.</param>
        /// <returns>Date with time.</returns>
        public static DateTime Create(DateTime date, int hour, int minute)
        {
            return new DateTime(date.Year, date.Month, date.Day, hour, minute, 0);
        }

        /// <summary>
        /// Creates the specified date with time.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <param name="time">The time in HH:mm format.</param>
        /// <returns>Date with time.</returns>
        public static DateTime Create(DateTime date, string time)
        {
            var parts = time.Split(':');

            if (parts.Length != 2)
            {
                throw new FormatException("Invalid format of time, must be HH:mm");
            }

            var hour = parts[0].To<int>();
            var minute = parts[1].To<int>();

            return Create(date, hour, minute);
        }
    }
}
